﻿using Microsoft.AspNet.Identity.EntityFramework;
using MVCForum.Mvc5.Identity.Models;
using mvcForum.DataProvider.EntityFramework;
using System;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using System.Data.Entity.ModelConfiguration.Configuration;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Infrastructure.Annotations;

namespace MVCForum.Mvc5.Identity.MvcForumIdentity {

	/// <summary>
	/// Entity Framework context, with room for you own custom entities
	/// </summary>
	public class IdentityDbContext : MVCForumContext {

		public IdentityDbContext() : base("DefaultConnection") { }
		public IdentityDbContext(String nameOrConnectionString) : base(nameOrConnectionString) { }

		/// <summary>
		/// Collection of ApplicationUsers
		/// </summary>
		public DbSet<ApplicationUser> Users { get; set; }
		/// <summary>
		/// Collection of IdentityRoles
		/// </summary>
		public DbSet<IdentityRole> Roles { get; set; }

		/// Put any custom entities you need for you application, adding them to the DbContext that is used
		/// by MVC Forum's Identity implementation, and below that, MVC Forum's core.

		protected override void OnModelCreating(DbModelBuilder modelBuilder) {
			base.OnModelCreating(modelBuilder);

			if (modelBuilder == null) {
				throw new ArgumentNullException("modelBuilder");
			}

			EntityTypeConfiguration<ApplicationUser> table = modelBuilder.Entity<ApplicationUser>().ToTable("ApplicationUser");
			table.HasMany<IdentityUserRole>((ApplicationUser u) => u.Roles).WithRequired().HasForeignKey<String>((IdentityUserRole ur) => ur.UserId);
			table.HasMany<IdentityUserClaim>((ApplicationUser u) => u.Claims).WithRequired().HasForeignKey<String>((IdentityUserClaim uc) => uc.UserId);
			table.HasMany<IdentityUserLogin>((ApplicationUser u) => u.Logins).WithRequired().HasForeignKey<String>((IdentityUserLogin ul) => ul.UserId);
			StringPropertyConfiguration stringPropertyConfiguration = table.Property((ApplicationUser u) => u.UserName).IsRequired().HasMaxLength(new int?(256));
			IndexAttribute indexAttribute = new IndexAttribute("UserNameIndex");
			indexAttribute.IsUnique = true;
			stringPropertyConfiguration.HasColumnAnnotation("Index", new IndexAnnotation(indexAttribute));
			table.Property((ApplicationUser u) => u.Email).HasMaxLength(new int?(256));

			modelBuilder.Entity<IdentityUserRole>().HasKey((IdentityUserRole r) => new { UserId = r.UserId, RoleId = r.RoleId }).ToTable("IdentityUserRole");

			modelBuilder.Entity<IdentityUserLogin>().HasKey((IdentityUserLogin l) => new { LoginProvider = l.LoginProvider, ProviderKey = l.ProviderKey, UserId = l.UserId }).ToTable("IdentityUserLogin");

			modelBuilder.Entity<IdentityUserClaim>().ToTable("IdentityUserClaim");

			EntityTypeConfiguration<IdentityRole> entityTypeConfiguration = modelBuilder.Entity<IdentityRole>().ToTable("IdentityRole");
			StringPropertyConfiguration stringPropertyConfiguration1 = entityTypeConfiguration.Property((IdentityRole r) => r.Name).IsRequired().HasMaxLength(new int?(256));
			IndexAttribute indexAttribute1 = new IndexAttribute("RoleNameIndex");
			indexAttribute1.IsUnique = true;
			stringPropertyConfiguration1.HasColumnAnnotation("Index", new IndexAnnotation(indexAttribute1));
			entityTypeConfiguration.HasMany<IdentityUserRole>((IdentityRole r) => r.Users).WithRequired().HasForeignKey<String>((IdentityUserRole ur) => ur.RoleId);
		}
	}
}